package cn.wgd.zmx.controller;

import cn.wgd.zmx.common.result.Response;
import cn.wgd.zmx.controller.c_model.UserModel;
import cn.wgd.zmx.domain.User;
import cn.wgd.zmx.service.UserService;
import cn.wgd.zmx.utils.TimeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api("用户管理控制器")
@RequestMapping("/user")
public class UserController {

    @Autowired
    @ApiModelProperty("注入userService")
    private UserService userService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("获取所有用户")
    public Response listUser(@RequestParam("page") String page, @RequestParam("limit") String limit) {
        return userService.list();
    }

    @RequestMapping(value = "del", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("删除指定用户")
    public Response delUser(@RequestParam(value = "id") Integer id) {
        if (id == null) {
            throw new IllegalArgumentException();
        }
        return userService.del(id);
    }

    @RequestMapping(value = "add", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("增加用户")
    public Response addUser(UserModel user) {
        if (user == null) {
            throw new IllegalArgumentException();
        }
        return userService.add(user);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("更新用户")
    public Response updateUser(UserModel user) {
        if (user == null) {
            throw new IllegalArgumentException();
        }
        return userService.updateUser(user);
    }

    @RequestMapping(value = "search",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation("搜索用户")
    public Response searchUser(@RequestParam("name")String name){
        if(StringUtils.isEmpty(name)){
            throw new IllegalArgumentException();
        }
        return userService.searchByName(name);
    }

}
